package com.foreveross.fetl.core.domain.freight.repository.impl;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;

import com.foreveross.fetl.common.db.hbase.HBaseCellModel;
import com.foreveross.fetl.core.domain.freight.hbase.AgentEntity;
import com.foreveross.fetl.core.domain.freight.hbase.CabinEntity;
import com.foreveross.fetl.core.domain.freight.hbase.TransitchangeEntity;

public class PlaneInfoHbaseUtils {

	private static Map<String,Method> agentMethods = new HashMap<String, Method>(); 

	static{
		Method[] agentMs = AgentEntity.class.getMethods();
		for (Method m : agentMs) {
			agentMethods.put(m.getName(), m);
		}
	}
	public static Set<CabinEntity> getCabinEntitySet(Set<HBaseCellModel> cellSet) {
		// 对仓位信息组织
		// 找出仓位最大的序号
		int max = 0;
		for (HBaseCellModel cell : cellSet) {
			String qualifier = cell.getQualifier();
			if (qualifier.startsWith("id_")) {
				int num = Integer.parseInt(qualifier.split("_")[1]);
				if (num > max) {
					max = num;
				}
			}
		}
		List<CabinEntity> cabinList = new ArrayList<CabinEntity>();

		for (int i = 0; i < max + 1; i++) {
			cabinList.add(new CabinEntity());
		}
		for (HBaseCellModel cell : cellSet) {
			String family = cell.getFamily();
			String qualifier = cell.getQualifier();
			if (family.startsWith("cabinsInfo")) {
				int index = Integer.parseInt(qualifier.split("_")[1]);
				if(cabinList.get(index) == null) continue;
				cabinList.get(index).setFlag(true);
				if (qualifier.startsWith("cabinType")) {
					cabinList.get(index).setCabinType(cell.getValue());
				}else if (qualifier.startsWith("productName")) {
					cabinList.get(index).setProductName(cell.getValue());
				}else if (qualifier.startsWith("subCabinName")) {
					cabinList.get(index).setSubCabinName(cell.getValue());
				} else if (qualifier.startsWith("price")) {
					String price = null;
					if (cell.getValue().contains("￥")) {
						price = cell.getValue().replace("￥", "");
					} else {
						price = cell.getValue();
					}
					if (StringUtils.isNotBlank(price) && NumberUtils.isNumber(price)) {
						cabinList.get(index).setPrice(Double.valueOf(price));
					}

				} 
			} 
		}
		Set<CabinEntity> cabinToList = new HashSet<CabinEntity>();
		for (CabinEntity cabin : cabinList) {
			if (cabin.isFlag()) {
				cabinToList.add(cabin);
			}
		}
		return cabinToList;
	}

	public static List<TransitchangeEntity> getTransitchangeEntityList(
			Set<HBaseCellModel> cellSet) {
		// 对中转信息组织
		// 找出中转最大的序号
		int max = 0;
		for (HBaseCellModel cell : cellSet) {
			String qualifier = cell.getQualifier();
			if (qualifier.startsWith("transit")) {
				int num = Integer.parseInt(qualifier.split("_")[1]);
				if (num > max) {
					max = num;
				}

			}
		}
		List<TransitchangeEntity> transitList = new ArrayList<TransitchangeEntity>();
		for (int i = 0; i < max + 1; i++) {
			transitList.add(new TransitchangeEntity());
		}
		for (HBaseCellModel cell : cellSet) {
			String qualifier = cell.getQualifier();
			if (qualifier.startsWith("transit")) {
				int index = Integer.parseInt(qualifier.split("_")[1]);
				// String cabinFlag = qualifier.split("_")[0];
				transitList.get(index).setFlag(true);
				if (qualifier.startsWith("transitCarrierFullName")) {
					transitList.get(index).setCarrierFullName(cell.getValue());
				} else if (qualifier.startsWith("transitCarrierKey")) {
					transitList.get(index).setCarrierKey(cell.getValue());
				} else if (qualifier.startsWith("transitCarrierName")) {
					transitList.get(index).setCarrierName(cell.getValue());
				} else if (qualifier.startsWith("transitEndTime")) {
					transitList.get(index).setEndTime(cell.getValue());
				} else if (qualifier.startsWith("transitFlightTyte")) {
					transitList.get(index).setFlightTyte(cell.getValue());
				} else if (qualifier.startsWith("transitFromAir")) {
					transitList.get(index).setFromAir(cell.getValue());
				} else if (qualifier.startsWith("transitHightPrice")) {
					if (checkString(cell.getValue())) {
						transitList.get(index).setHightPrice(
								Double.parseDouble(cell.getValue()));
					} else {
						transitList.get(index).setHightPrice(0d);
					}
				} else if (qualifier.startsWith("transitLowerPrice")) {
					if (checkString(cell.getValue())) {
						transitList.get(index).setLowerPrice(
								Double.parseDouble(cell.getValue()));
					} else {
						transitList.get(index).setLowerPrice(0d);
					}
				} else if (qualifier.startsWith("transitStartTime")) {
					transitList.get(index).setStartTime(cell.getValue());
				} else if (qualifier.startsWith("transitToAir")) {
					transitList.get(index).setToAir(cell.getValue());
				} else if (qualifier.startsWith("transitFlightNo")) {
					transitList.get(index).setFlightNo(cell.getValue());
				}
			}
		}
		List<TransitchangeEntity> list = new ArrayList<TransitchangeEntity>();
		for (TransitchangeEntity transitchangeEntity : transitList) {
			if (transitchangeEntity.isFlag()) {
				list.add(transitchangeEntity);
			}
		}
		return list;
	}

	public static Set<AgentEntity> getAgent(Set<HBaseCellModel> cells) {
		Set<AgentEntity> agents = new HashSet<AgentEntity>();
		Map<String, AgentEntity> agentMap = new HashMap<String, AgentEntity>();
		String[] agentInfoKey;
		try {
			for (HBaseCellModel cell : cells) {
				String family = cell.getFamily();
				if ("agentInfos".equals(family)) {
					agentInfoKey = cell.getQualifier().split("_");
					String property = agentInfoKey[0];
					String str = agentInfoKey[1];
					Method method = agentMethods.get("set"
							+ property.substring(0, 1).toUpperCase()
							+ property.substring(1));
					if (null != method) {
						if (!agentMap.containsKey(str)) {
							agentMap.put(str, new AgentEntity());
						}
						if (agentMap.keySet().contains(str))
							method.invoke(agentMap.get(str), cell.getValue());
					}
				}
			}
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		agents.addAll(agentMap.values());
		return agents;
	}

	private static boolean checkString(String str) {
		if (str != null && !"".equals(str.trim())) {
			return true;
		} else {
			return false;
		}
	}
}
